using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AchiveManager : MonoBehaviour
{
    public GameObject[] lockCharacters;
    public GameObject[] unlockCharacters;
    public GameObject uiNotice;

    enum Achive { UnlockPurple, UnlockOrange}
    Achive[] achives;

    WaitForSecondsRealtime wait;

    private void Awake()
    {
        achives = (Achive[])Enum.GetValues(typeof(Achive));
        wait = new WaitForSecondsRealtime(3);
        if (!PlayerPrefs.HasKey("MyData"))
        {
            Init();
        }
       
    }

    // Start is called before the first frame update
    void Start()
    {
        UnLockCharacters();
    }

    // Update is called once per frame
    void Update()
    {
        foreach (Achive achive in achives)
        {
            CheckAchive(achive);
        }
    }

    private void Init()
    {
        PlayerPrefs.SetInt("MyData", 1);
        //PlayerPrefs.SetInt("UnlockPurple", 0);
        //PlayerPrefs.SetInt("UnlockOrange", 0);
        foreach (Achive achive in achives)
        {
            PlayerPrefs.SetInt(achive.ToString(), 0);
        }
    }

    private void UnLockCharacters()
    {
        for (int i = 0; i < lockCharacters.Length; i++)
        {
            string achiveName = achives[i].ToString();
            bool isUnlock = PlayerPrefs.GetInt(achiveName) == 1;
            lockCharacters[i].SetActive(!isUnlock);
            unlockCharacters[i].SetActive(isUnlock);
        }
    }

    private void CheckAchive(Achive achive)
    {
        bool isAchive = false;
        switch (achive)
        {
            case Achive.UnlockPurple:
                isAchive = GameManager.instance.kill >= 10;
                break;
            case Achive.UnlockOrange:
                isAchive = GameManager.instance.gameTime == GameManager.instance.maxGameTime;
                break;
        }

        if(isAchive && PlayerPrefs.GetInt(achive.ToString()) == 0)
        {
            PlayerPrefs.SetInt(achive.ToString(),1);
            for (int i = 0; i < uiNotice.transform.childCount; i++)
            {
                bool isActive = i == (int)achive;
                uiNotice.transform.GetChild(i).gameObject.SetActive(isActive);
            }
            StartCoroutine(NoticeRoutine());
        }
    }

    IEnumerator NoticeRoutine()
    {
        uiNotice.SetActive(true);
        AudioManager.instance.PlaySfx(AudioManager.Sfx.LevelUp);

        yield return wait;
        uiNotice.SetActive(false);
    }
}
